package com.hangzhou.xyj.ui.activity;

import android.content.res.AssetManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.EditText;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.BuildInfoAdapter;
import com.hangzhou.xyj.ui.widget.DividerItemDecoration;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.constants.Constants;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class BuildInfoActivity extends BaseActivity {

    private RecyclerView mRecyclerView;
    private EditText mEtServerAddress;
    private Button mBtnSaveServer;

    private BuildInfoAdapter mAdapter;
    private List<String> mBuildInfoList = new ArrayList<>();



    @Override
    public void initTop() {
        enableTitle(true, "编译信息");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.layout_common_recycleview);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_common);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置水平分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
                , R.drawable.line_divider_one));
        mRecyclerView.setAdapter(mAdapter = new BuildInfoAdapter(mContext, mBuildInfoList));
    }

    @Override
    public void initDatas() {
        mBuildInfoList.clear();
        addAppRunInfo();
        if (readFile() != null) {
            mBuildInfoList.addAll(readFile());
        }
    }

    @Override
    public void initListeners() {

    }


    private void initView() {

        //以下是切换服务器地址
//        mEtServerAddress = (EditText) findViewById(R.id.et_server_address);
//        mBtnSaveServer = (Button) findViewById(R.id.btn_save_server);

//        mEtServerAddress.setText(PreferenceUtil.getCachedHostAPI());

        //NOTE(dfb):长按切换服务器地址
//        findViewById(R.id.btn_select_server_address).setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                selectServerAddress();
//                return true;
//            }
//        });
    }

//    private void saveServerAddress(String ip) {
//        try {
//            URL url = new URL(ip);
//            Configuration.APPURL = ip;
//            HostItem host = PreferenceUtil.getCachedHost();
//            if (host != null) {
//                host.host = ip;
//                PreferenceUtil.saveCachedHost(host);
//            }
//            ToastUtil.showMessage(mContext, getString(R.string.tip_error_host_save_ok));
//        } catch (Exception e) {
//            mEtServerAddress.setError(getString(R.string.tip_error_host_not_url));
//            return;
//        }
//    }

    private List<String> readFile() {
        List<String> infoList;
        try {
            infoList = new ArrayList<>();
            AssetManager assetManager = this.getAssets();
            InputStream inputStream = assetManager.open("info.txt");

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                infoList.add(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
            infoList = null;
        }

        return infoList;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    private void addAppRunInfo() {
        mBuildInfoList.add(String.format("渠道号 = %s", PublicUtil.getChannelMeta(mContext)));
        mBuildInfoList.add(String.format("服务器地址 = " + Constants.API_URL));
    }

    /**
     * 选择ip对话框
     */
//    private void selectServerAddress() {
//        AlertDialog.Builder iPBuilder = new AlertDialog.Builder(this).setTitle("running:" + PreferenceUtil.getCachedHostAPI())
//                .setItems(API.Server.IP_ARRAY, new DialogInterface.OnClickListener() {
//
//                    @Override
//                    public void onClick(DialogInterface dialog, int position) {
//                        saveServerAddress(API.Server.IP_ARRAY[position]);
//                    }
//                });
//        iPBuilder.show();
//    }
}
